package com.qianli.cashmama.trade.helper;

import com.fqgj.xjd.user.client.enums.UserContactFamilyTypeEnum;
import com.fqgj.xjd.user.client.enums.UserContactFriendTypeEnum;
import com.fqgj.xjd.user.client.request.UserEmergencyContact;
import com.fqgj.xjd.user.client.response.UserFamilyContact;
import com.fqgj.xjd.user.client.response.UserFriendContact;
import com.qianli.cashmama.trade.client.snapshot.UserTradeSnapshotService;
import com.qianli.cashmama.trade.client.snapshot.response.UserSnapInfoResponse;
import com.qianli.cashmama.trade.common.enums.BizTypeEnum;
import com.qianli.cashmama.trade.common.enums.TradeBorrowDurationEnum;
import com.qianli.cashmama.trade.domain.model.BizRecord;
import com.qianli.cashmama.trade.domain.model.TradeBank;
import com.qianli.cashmama.trade.domain.model.TradeBase;
import com.qianli.cashmama.trade.integration.pay.dto.TradePayDto;
import com.qianli.cashmama.trade.repo.BizRecordRepository;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2017/11/1
 * Time: 下午3:32
 */
@Component
public class LoanDataCreateHelper {
    private static final Logger LOGGER = LoggerFactory.getLogger(LoanDataCreateHelper.class);

    @Autowired
    private UserTradeSnapshotService userTradeSnapshotService;

    @Autowired
    private BizRecordRepository bizRecordRepository;

    /**
     * @param tradeBase
     * @param bizNoTmp
     * @return
     */
    public TradePayDto createTradePayDto(TradeBase tradeBase, String bizNoTmp) {

        String tradeNo = tradeBase.getTradeNo();
        TradeBank tradeBank = tradeBase.getTradeBank();
        tradeBase.setTradeBank(tradeBank);

        BizRecord bizRecord = new BizRecord()
                .setBizType(BizTypeEnum.TRADE_TO_PAY)
                .setInnerNo(tradeBase.getTradeNo())
                .setBizNo(tradeBase.getTradeNo());
        if (StringUtils.isNotEmpty(bizNoTmp)) {
            bizRecord.setBizNo(bizNoTmp);
        }
        String bizNo = bizRecord.getBizNo();
        bizRecordRepository.store(bizRecord);
        TradePayDto tradePayDto = new TradePayDto()
                .setBizNo(bizNo)
                .setTradeNo(tradeNo)
                .setTradeId(tradeBase.getTradeId())
                .setUserCode(tradeBase.getUserCode())

                .setSignMobile(tradeBank.getSignMobile())
                .setUserName(tradeBank.getUserName())
                .setBankName(tradeBank.getBankName())
                .setBankNo(tradeBank.getCardNo())

                .setBankType(tradeBase.getTradeBank().getBankType())
                .setIdentityNo(tradeBank.getIdentityNo())
                .setBorrowCapital(tradeBase.getBorrowTotalCapital())
                .setBorrowDurationEnum(TradeBorrowDurationEnum.convertByType(tradeBase.getBorrowDuration()))

                .setPeriodLength(tradeBase.getPeriodLength())
                .setTotalPeriod(tradeBase.getTotalPeriod())
                .setReceivedCapital(tradeBase.getReceivedTotalCapital())
                .setProductCode(tradeBase.getProductCode())
                .setBorrowRatio(tradeBase.getBorrowRatio())
                .setTotalServiceFee(tradeBase.getTotalServiceFee())
                .setDocumentId(tradeBase.getDocumentId())
                .setContractUrl(tradeBase.getContractUrl())
                .setSignDate(tradeBase.getSignDate())
                .setAuditDate(tradeBase.getAuditDate())
                .setCapitalCode(tradeBase.getCapitalCode());

        try {
            UserSnapInfoResponse userSnapInfo = userTradeSnapshotService.getTradeUserSnapInfo(tradeNo).getData();

            if (userSnapInfo != null) {
                UserEmergencyContact userEmergencyContact = new UserEmergencyContact();
                // 用户家庭联系人
                UserFamilyContact userFamilyContact = new
                        UserFamilyContact()
                        .setUserContactFamilyTypeEnum(UserContactFamilyTypeEnum.getEnumByDesc(userSnapInfo.getFamilyEmergenceContactRelation()))
                        .setMobile(userSnapInfo.getFamilyEmergenceContactMobile())
                        .setContactName(userSnapInfo.getFamilyEmergenceContactName())
                        .setName(userSnapInfo.getFamilyEmergenceContactInputName());
                // 用户朋友联系人
                UserFriendContact userFriendContact = new UserFriendContact()
                        .setUserContactFriendTypeEnum(UserContactFriendTypeEnum.getEnumByDesc(userSnapInfo.getFriendEmergenceContactRelation()))
                        .setMobile(userSnapInfo.getFriendEmergenceContactMobile())
                        .setContactName(userSnapInfo.getFriendEmergenceContactName())
                        .setName(userSnapInfo.getFriendEmergenceContactInputName());
                // 用户紧急联系人
                userEmergencyContact.setUserFamilyContact(userFamilyContact).setUserFriendContact(userFriendContact);

                tradePayDto.setUserCity(userSnapInfo.getLiveCity())
                        .setUserProvince(userSnapInfo.getLiveProvince())
                        .setUserAddress(userSnapInfo.getLiveAddress())
                        .setCompanyName(userSnapInfo.getUserCompanyName())
                        .setMarriageStatus(userSnapInfo.getMarriageStatus())
                        .setIdFrontUrl(userSnapInfo.getIdentityFrontUrl())
                        .setIdBackUrl(userSnapInfo.getIdentityBackUrl())
                        .setLivePicUrl(userSnapInfo.getLivePictureUrl())
                        .setUserEmergencyContact(userEmergencyContact)
                        .setEmail(userSnapInfo.getUserEmail());
            } else {
                LOGGER.error("== 没有用户信息快照 ==,tradeNo:{}", tradeNo);
            }
            tradePayDto.setZmScore("620");
        } catch (Exception e) {
            LOGGER.error("== 生成支付的额外信息发生异常 ==,tradeNo:{},e", tradeNo, e);
        }
        return tradePayDto;
    }
}
